<?php

namespace User\Controller;

use Think\Controller;


/*  log表type
 * 1'点位奖励' 2 '推荐奖励'3 '进入复投钱包' 4'占位支付'5 '进入奖池'6 '进入业绩比拼' 7 '归平台部分'8 '奖池结算'9 '业绩结算''10 '充值''11'提现'
 */

class UserController extends BaseController
{

  var $userObj;

  public function __construct()
  {
    parent::__construct();
    $this->userObj = D('User');
    $sys = M('Sys')->select();
    foreach ($sys as $k => $v) {
      if ($v['name'] == 'currency_name') {
        $this->assign('B', $v['value']);
      } elseif ($v['name'] == 'list') {
        $this->assign('now_list', $v['value']);
      } elseif ($v['name'] == 'jc_text') {
        $arr = explode("<>", $v['value']);
        $arr1 = array();
        foreach($arr as $k => $v){
           $arr1[$k]['text'] = $v;
        }
        $this->assign('jc_text', $arr1);
      } else {
        $this->assign('' . $v['name'], $v['value']);
      }
    }
    $this->assign('lang', $this->lang("zh"));
    $userId = trim(session('userId'));
    $status = M("User")->where(array("id"=>$userId))->getField("status");
    if($status == 1){
      $this->error('您的账号已被冻结！', U('User/Index/logout'));
    }
    $username = M("User")->where(array("id" => $userId))->getField("username");
    $this->assign("uid", $userId);
    $this->assign("user_name", $username);
  }

  public function index()
  {
    $xgmm = I("xgmm");
    if($xgmm == 1){
      $this->assign("xgmm", $xgmm);
    }
    //占位费用
    $seat_money = M("Sys")->where(array("name"=>"seat_money"))->getField("value");
    $this->assign("seat_money", $seat_money);


    $in_pond = M("Sys")->where(array("name"=>"in_pond"))->getField("value");
    $pond = M("Sys")->where(array("name"=>"pond"))->getField("value");
    $seat_on = M("Sys")->where(array("name"=>"seat_on"))->getField("value");
    if($seat_on == 1 && $pond == 0){
      $d['value'] = $in_pond*15;
      M("Sys")->where(array("name"=>"pond"))->save($d);
      $this->assign("pond", $in_pond*15);
    }




    /*最后一位占位者*/
    $lasttime = M("Sys")->where(array("name"=>"now_list_time"))->getField("value");
    /*奖池周期*/
    $jz_countdown = M("Sys")->where(array("name"=>"jz_countdown"))->getField("value");
    //转换为秒
    $countdown =  $jz_countdown*60*60;
    //结束时间
    $endtime = $lasttime+$countdown;
    /*剩余时间*/
    $second = $endtime - time();
    $this->assign("second",$second);
    $day = floor($second / (3600 * 24));
    $second = $second % (3600 * 24);
    $hour = floor($second / 3600) + $day*24 ;
    $second = $second % 3600;
    $minute = floor($second / 60);
    $second = $second % 60;
    $shengyu = $hour . ":" . $minute . ":" . $second;
    $this->assign("shengyu", $shengyu);

    $seatlist = M("Seatlist")->order("id desc")->limit(3)->select();
    foreach ($seatlist as $k => $v) {
      $username = M("User")->where(array("id" => $v['user_id']))->getField("username");
      $seatlist[$k]['username'] = change_username($username);
    }
    $this->assign("seatlist", $seatlist);
    $user = M("User");
    $userId = trim(session('userId'));
    $userinfo = $user->where("id=$userId")->select();
    $userinfo[0]['usermoney'] = sctonum($userinfo[0]['usermoney']);
    $userinfo[0]['freeze_money'] = sctonum($userinfo[0]['freeze_money']);
    //团队活跃度
    $users = M("User")->where(array("shiming"=>1))->select();
    //团队所有会员
    $childs = GetTeamMember($users,$userId);
    $childs = implode(',',$childs);
    if($childs){
      $jichu = M("User")->where(array("id"=>array("in",$childs)))->sum('huoyue');//基础活跃度
      $jichu = $jichu?$jichu:0;
      $jiacheng = M("User")->where(array("id"=>array("in",$childs)))->sum('huoyue_jc');//加成活跃度
      $tdhyd = $jichu + $jiacheng;//团队活跃度 = 基础活跃度+加成活跃度
    }

    $userinfo[0]['huoyue_tuan'] = $tdhyd?$tdhyd:0;


    $childnum = M("User")->where("parent = $userId")->count("id");
    $this->assign("childnum", $childnum);
    $this->assign("userinfo", $userinfo);
    //点位奖
    $j1 = M("Log")->where(array("user_id" => $userId, "type" => 1))->getField("amount", true);
    $j1 = ypp_array_sum($j1);
    //$j1 = (array_sum($j1) > 0) ? array_sum($j1) : 0;

    //推荐奖
    $j2 = M("Log")->where(array("user_id" => $userId, "type" => array("in","2,21")))->getField("amount", true);
    $j2 = ypp_array_sum($j2);
    //$j2 = (array_sum($j2) > 0) ? array_sum($j2) : 0;
    //奖池结算
    $j3 = M("Log")->where(array("user_id" => $userId, "type" => 8))->getField("money", true);
    $j3 = ypp_array_sum($j3);
    //$j3 = (array_sum($j3) > 0) ? array_sum($j3) : 0;
    //业绩结算
    $j4 = M("Log")->where(array("user_id" => $userId, "type" => 9))->getField("money", true);
    $j4 = ypp_array_sum($j4);
    //$j4 = (array_sum($j4) > 0) ? array_sum($j4) : 0;

    //团队奖
    $j5 = M("Log")->where(array("user_id" => $userId, "type" => 23))->getField("amount", true);
    $j5 = ypp_array_sum($j5);

    //矿机收益
    $j6 = M("Log")->where(array("user_id" => $userId, "type" => 22))->getField("amount", true);
    $j6 = ypp_array_sum($j6);

    //理财收益
    $j7 = M("Log")->where(array("user_id" => $userId, "type" => 25))->getField("amount", true);
    $j7 = ypp_array_sum($j7);

    $this->assign('j1', $j1);
    $this->assign('j2', $j2);
    $this->assign('j3', $j3);
    $this->assign('j4', $j4);
    $this->assign('j5', $j5);
    $this->assign('j6', $j6);
    $this->assign('j7', $j7);

    $this->assign('j0', $j1+$j2+$j3+$j4+$j5+$j6+$j7);


    $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
    $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
    $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
    $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
    //昨天点位奖
    $y_day_seat = M("Log")->where(array("user_id" => $userId, "type" => 1, "date" => array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->getField("amount", true);
    $y_day_seat = ypp_array_sum($y_day_seat);
    //$y_day_seat = !empty(array_sum($y_day_seat)) ? array_sum($y_day_seat) : 0;
    //今天点位奖
    $t_day_seat = M("Log")->where(array("user_id" => $userId, "type" => 1, "date" => array(array('gt', $beginToday), array('lt', $endToday), 'and')))->getField("amount", true);
    $t_day_seat = ypp_array_sum($t_day_seat);
    //$t_day_seat = !empty(array_sum($t_day_seat)) ? array_sum($t_day_seat) : 0;
    //全部点位奖
    $all_seat = M("Log")->where(array("user_id" => $userId, "type" => 1))->getField("amount", true);
    $all_seat = ypp_array_sum($all_seat);
    //$all_seat = !empty(array_sum($all_seat)) ? array_sum($all_seat) : 0;

    //昨天推荐奖
    $y_day_tj = M("Log")->where(array("user_id" => $userId, "type" => array("in","2,21"), "date" => array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->getField("amount", true);
    $y_day_tj = ypp_array_sum($y_day_tj);
    //$y_day_tj = !empty(array_sum($y_day_tj)) ? array_sum($y_day_tj) : 0;
    //今天推荐奖
    $t_day_tj = M("Log")->where(array("user_id" => $userId, "type" => array("in","2,21"), "date" => array(array('gt', $beginToday), array('lt', $endToday), 'and')))->getField("amount", true);
    $t_day_tj = ypp_array_sum($t_day_tj);
    //$t_day_tj = !empty(array_sum($t_day_tj)) ? array_sum($t_day_tj) : 0;
    //全部推荐奖
    $all_tj = M("Log")->where(array("user_id" => $userId, "type" => array("in","2,21")))->getField("amount", true);
    $all_tj = ypp_array_sum($all_tj);
    //$all_tj = !empty(array_sum($all_tj)) ? array_sum($all_tj) : 0;

    //昨天团队奖
    $y_day_td = M("Log")->where(array("user_id" => $userId, "type" => 23, "date" => array(array('egt', $beginYesterday), array('lt', $endYesterday), 'and')))->getField("amount", true);
    $y_day_td = ypp_array_sum($y_day_td);
    //今天团队奖
    $t_day_td = M("Log")->where(array("user_id" => $userId, "type" => 23, "date" => array(array('egt', $beginToday), array('lt', $endToday), 'and')))->getField("amount", true);
    $t_day_td = ypp_array_sum($t_day_td);
    //全部团队奖
    $all_td = M("Log")->where(array("user_id" => $userId, "type" => 23))->getField("amount", true);
    $all_td = ypp_array_sum($all_td);


    //昨天矿机收益
    $y_day_kj = M("Log")->where(array("user_id" => $userId, "type" => 22, "date" => array(array('egt', $beginYesterday), array('lt', $endYesterday), 'and')))->getField("amount", true);
    $y_day_kj = ypp_array_sum($y_day_kj);
    //今天矿机收益
    $t_day_kj = M("Log")->where(array("user_id" => $userId, "type" => 22, "date" => array(array('egt', $beginToday), array('lt', $endToday), 'and')))->getField("amount", true);
    $t_day_kj = ypp_array_sum($t_day_kj);
    //全部矿机收益
    $all_kj = M("Log")->where(array("user_id" => $userId, "type" => 22))->getField("amount", true);
    $all_kj = ypp_array_sum($all_kj);

    //昨天理财收益
    $y_day_lc = M("Log")->where(array("user_id" => $userId, "type" => 25, "date" => array(array('egt', $beginYesterday), array('lt', $endYesterday), 'and')))->getField("amount", true);
    $y_day_lc = ypp_array_sum($y_day_lc);
    //今天理财收益
    $t_day_lc = M("Log")->where(array("user_id" => $userId, "type" => 25, "date" => array(array('egt', $beginToday), array('lt', $endToday), 'and')))->getField("amount", true);
    $t_day_lc = ypp_array_sum($t_day_lc);
    //全部理财收益
    $all_lc = M("Log")->where(array("user_id" => $userId, "type" => 25))->getField("amount", true);
    $all_lc = ypp_array_sum($all_lc);


    //昨天其它奖
    $y_day_qt = M("Log")->where(array("user_id" => $userId, "type" => array("in", "8,9"), "date" => array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->getField("money", true);

    $y_day_qt = !empty(array_sum($y_day_qt)) ? array_sum($y_day_qt) : 0;
    //今天其它奖
    $t_day_qt = M("Log")->where(array("user_id" => $userId, "type" => array("in", "8,9"), "date" => array(array('gt', $beginToday), array('lt', $endToday), 'and')))->getField("money", true);
    $t_day_qt = !empty(array_sum($t_day_qt)) ? array_sum($t_day_qt) : 0;
    //全部其它奖
    $all_qt = M("Log")->where(array("user_id" => $userId, "type" => array("in", "8,9")))->getField("money", true);
    $all_qt = !empty(array_sum($all_qt)) ? array_sum($all_qt) : 0;


    $this->assign('y_day_seat', $y_day_seat);
    $this->assign('t_day_seat', $t_day_seat);
    $this->assign('all_seat', $all_seat);

    $this->assign('y_day_tj', $y_day_tj);
    $this->assign('t_day_tj', $t_day_tj);
    $this->assign('all_tj', $all_tj);

    $this->assign('y_day_td', $y_day_td);
    $this->assign('t_day_td', $t_day_td);
    $this->assign('all_td', $all_td);

    $this->assign('y_day_kj', $y_day_kj);
    $this->assign('t_day_kj', $t_day_kj);
    $this->assign('all_kj', $all_kj);

    $this->assign('y_day_lc', $y_day_lc);
    $this->assign('t_day_lc', $t_day_lc);
    $this->assign('all_lc', $all_lc);

    $this->assign('y_day_qt', $y_day_qt);
    $this->assign('t_day_qt', $t_day_qt);
    $this->assign('all_qt', $all_qt);

    $this->assign('all_y_day', $y_day_seat + $y_day_tj + $y_day_td  + $y_day_kj  + $y_day_lc + $y_day_qt);
    $this->assign('all_t_day', $t_day_seat + $t_day_tj + $t_day_td  + $t_day_kj  + $t_day_lc + $t_day_qt);
    $this->assign('all_all', $all_seat + $all_tj + $all_td  + $all_kj  + $all_lc + $all_qt);

    $sysnotice = M("Sysnotice")->order("date desc")->limit(3)->select();
    $sysnotices = M("Sysnotice")->order("date desc")->limit(1)->select();

    //YPP 显示系统公告
    $today = strtotime(date("Ymd",time()));
    $closemsg =  M("User")->where(array("id"=>$userId))->getField('close_msg');
    if($closemsg < $today){
      $this->assign('showmsg', 1);
    }else{
      $this->assign('showmsg', 0);
    }

    $this->assign('sysnotice', $sysnotice);
    $this->assign('sysnotices', $sysnotices);
    $this->assign('is_mobile', $this->ismobile());
    $this->display();
  }



  /*系统公告详情*/
  public function sysnotice_detail()
  {
    $id = I("get.id");
    $sysnotice = M("Sysnotice")->where("id=$id")->select();
    $this->assign('sysnotice', $sysnotice);
    $this->display();
  }

  /*关闭系统公告*/
  public function closemsg()
  {
    $userId = trim(session('userId'));
    $u['close_msg'] = time();
    $res = M("User")->where(array("id"=>$userId))->save($u);
    if($res){
      $data['error'] = 1;
      $data['msg'] = "已关闭";
      $this->ajaxReturn($data, 'JSON');
    }
  }

  public function rank_list()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 20; //每页显示条数
    $this->assign("pagenum", $pagenum);

    $userlist = M("User")->select();
    //业绩比拼，限定最少推荐人数
    $rank_num = M("Sys")->where(array("name" => 'rank_num'))->getField('value');
    //业绩比拼，限定前X名。
    $rank = M("Sys")->where(array("name" => 'ranks'))->getField('value');
    if($page*$pagenum > $rank){
      header('Location:/index.php?m=User&c=User&a=rank_list&page=1');
    }


    $list = array();
    foreach ($userlist as $k => $v) {
      if ($v['round_child_num'] >= $rank_num) {
        $isseat = M("Seatlist")->where(array("user_id" => $v['id']))->getField('id');
        if ($isseat > 0) {  //推荐者必须有过付费占位记录。
          $user_name = M("User")->where(array("id" => $v['id']))->getField('username');
          $user_name = change_username($user_name);
          $list[] = array('user' => $user_name, 'num' => $v['round_child_num'], 'time' => $v['activate_time']);
        }
      }
    }
    array_multisort(array_column($list, 'num'), SORT_DESC, array_column($list, 'time'), SORT_ASC, $list); /*按推荐数、占位时间排序*/
    $all_list = array_slice($list, 0,$rank); /*取排名前X*/
    $list = array_slice($list, $page*$pagenum-$pagenum,$pagenum); /*取排名前X*/
    $this->assign('list', $list);

    //ypp 分页
    $page_all = ceil(count($all_list)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("page1", $page-1);
    $this->assign("count", count($all_list));
    $this->display();
  }


  public function sysmsg_list()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 20; //每页显示条数
    $this->assign("pagenum", $pagenum);


    $sysnotice = M("Sysnotice")->order("id desc")->select();
    $all_list = $sysnotice; /*取排名前X*/
    $list = array_slice($sysnotice, $page*$pagenum-$pagenum,$pagenum); /*取排名前X*/
    $this->assign('list', $list);

    //ypp 分页
    $page_all = ceil(count($all_list)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("page1", $page-1);
    $this->assign("count", count($all_list));
    $this->display();
  }
  /*资金明细*/
  public function zj_list()
  {

    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    $status = !empty(I("status")) ? I("status") : 0;
    if(empty(I('page'))){
      $_SESSION["start_time"] = '';
      $_SESSION["end_time"] = '';
      $_SESSION["status"] = '';
    }

    $user_id = trim(session('userId'));
    $user = M("User")->where(array("id" => $user_id))->find();
    $user_money = M("User")->where(array("id" => $user_id))->getField("usermoney");
    $user_money = sctonum($user_money);
    $this->assign("user_money", $user_money);

    if (I("post.reservatTime") || $_SESSION['start_time'] !="") {
      if(I("post.reservatTime")){
        $_SESSION['start_time'] = I("post.reservatTime");
      }
      $s_time = strtotime($_SESSION['start_time']); //开始日期
      $this->assign("start_time", $_SESSION['start_time']);
    }
    if (I("post.leaveTime") || $_SESSION['end_time'] !="") {
      if(I("post.leaveTime")){
      $_SESSION['end_time'] = I("post.leaveTime");
      }
      $e_time = strtotime($_SESSION['end_time']) + 86400;    //截至日期当天23：59：59
      $this->assign("end_time", $_SESSION['end_time']);
    }

    if (I("status") || $_SESSION['status'] !="") {
      if(I("status")){
        $_SESSION['status'] = I("status");
      }
      $this->assign("status", $_SESSION['status']);
    }

    if (!$s_time) {
      $s_time = 0;
    }
    if (!$e_time) {
      $e_time = time();
    }
    $where = array("date >= $s_time", "date <= $e_time");

    if ($status == 0) {
      $all_loglist = M("Log")->where(array("user_id" => $user_id))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }elseif($status == 1){
      $all_loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0)))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0)))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }elseif($status == 2){
      $all_loglist = M("Log")->where(array("user_id" => $user_id,"huoyue"=>array("neq",0)))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id,"huoyue"=>array("neq",0)))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }elseif($status == 3){
      $all_loglist = M("Log")->where(array("user_id" => $user_id,"huoyue_jc"=>array("neq",0)))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id,"huoyue_jc"=>array("neq",0)))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }else{
      $this->error('参数错误！', U('User/zj_list'));
    }

    //ypp 分页
    $page_all = ceil($all_loglist/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $all_loglist);
    $this->assign('is_mobile', $this->ismobile());
    foreach($loglist as $k=>$v){
      $loglist[$k]['money'] = sctonum($loglist[$k]['money']);
      $loglist[$k]['huoyue'] = ($v['huoyue']>0)?$v['huoyue']:$v['huoyue_jc'];
      $loglist[$k]['sxf'] = M("Log")->where(array("type"=>12,"date"=>$v['date']))->getField("money");
    }
    $this->assign("loglist", $loglist);
    $this->assign("user", $user);
    $this->display("User/mymoney");
  }

  /*反馈*/
  public function msg()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    $user_id = trim(session('userId'));
    $all_msglist = M("Msg")->where(array("user_id" => $user_id))->field('*')->order("id desc")->group('msg_id')->select();
    $msglist = M("Msg")->where(array("user_id" => $user_id))->field('*')->order("id desc")->group('msg_id')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    $this->assign("msglist", $msglist);
    $this->assign("msgcount", count($msglist));

    //ypp 分页
    $page_all = ceil(count($all_msglist)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($all_msglist));


    $this->display();
  }

  /*反馈详情*/
  public function msgdetail()
  {
    $id = I("id");
    $user_id = trim(session('userId'));

    $msg_id = M("Msg")->where(array("id" => $id,'user_id'=>$user_id))->getField("msg_id");
    if(!$msg_id){
      $this->error('参数错误！', U('User/msg'));
    }

    $title = M("Msg")->where(array("id" => $id,'user_id'=>$user_id))->getField("title");
    if($title==""){
      $this->error('参数错误！', U('User/msg'));
    }

    $msg = M("Msg")->where(array("msg_id" => $msg_id))->order("id asc")->select();
    $title = M("Msg")->where(array("id" => $id))->getField("title");
    $status = M("Msg")->where(array("id" => $id))->getField("status");

    $this->assign("status", $status);
    $this->assign("msglist", $msg);
    $this->assign("title", $title);
    $this->display();
  }

  public function feedback()
  {
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 3145728;// 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'pdf');// 设置附件上传类型
    $upload->rootPath = './Uploads/'; // 设置附件上传根目录
    $upload->savePath = ''; // 设置附件上传（子）目录
    // 上传文件
    $info = $upload->upload();

    if ($info) {// 上传错误提示错误信息
      foreach ($info as $file) {
        $bigimg[] = $file['savepath'] . $file['savename'];
      }
    }
    $data['msg_id'] = time();
    $data['user_id'] = trim(session('userId'));
    $data['title'] = I("post.title");
    $data['content'] = str_replace("<","<\\",I("post.feedback"));
    $data['img'] = $bigimg[0];
    $data['date'] = time();
    $res = M("Msg")->add($data);
    if ($res) {
      $this->success('反馈已提交', U('User/msg'));
    }
  }

  public function re_feedback()
  {
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 3145728;// 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'pdf');// 设置附件上传类型
    $upload->rootPath = './Uploads/'; // 设置附件上传根目录
    $upload->savePath = ''; // 设置附件上传（子）目录
    // 上传文件
    $info = $upload->upload();

    if ($info) {// 上传错误提示错误信息
      foreach ($info as $file) {
        $bigimg[] = $file['savepath'] . $file['savename'];
      }
    }
    $data['msg_id'] = I("post.msg_id");
    $data['user_id'] = trim(session('userId'));
    $data['content'] = str_replace("<","<\\",I("post.re_feedback"));
    $data['img'] = $bigimg[0];
    $data['date'] = time();
    $res = M("Msg")->add($data);
    if ($res) {
      $this->success('反馈已提交', U('User/msg'));
    }
  }

  /*收益明细*/

  /*1.点位奖，2.推荐奖，8，奖池结算，9，业绩结算*/
  public function loglist()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $user_id = trim(session('userId'));
    $status = !empty(I("status")) ? I("status") : 0;

    if(empty(I('page'))){
      $_SESSION["start_time"] = '';
      $_SESSION["end_time"] = '';
      $_SESSION["status"] = '';
    }

    if (I("post.reservatTime") || $_SESSION['start_time'] !="") {
      if(I("post.reservatTime")){
        $_SESSION['start_time'] = I("post.reservatTime");
      }
      $s_time = strtotime($_SESSION['start_time']); //开始日期
      $this->assign("start_time", $_SESSION['start_time']);
    }
    if (I("post.leaveTime") || $_SESSION['end_time'] !="") {
      if(I("post.leaveTime")){
        $_SESSION['end_time'] = I("post.leaveTime");
      }
      $e_time = strtotime($_SESSION['end_time']) + 86400;    //截至日期当天23：59：59
      $this->assign("end_time", $_SESSION['end_time']);
    }
    if (I("status") || $_SESSION['status'] !="") {
      if(I("status")){
        $_SESSION['status'] = I("status");
      }
      $this->assign("status", $_SESSION['status']);
    }

    if (!$s_time) {
      $s_time = 0;
    }
    if (!$e_time) {
      $e_time = time();
    }
    $where = array("date >= $s_time", "date <= $e_time");
    if ($status == 0) {
      $all_loglist =  M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "1,2,7,8,9,21,22,23,25")))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "1,2,7,8,9,21,22,23,25")))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    } else if ($status == 2) {
      $all_loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "2,21")))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "2,21")))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }
    else if ($status == 3) {
      $all_loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "7,8,9")))->where($where)->order('id desc')->count();
      $loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "7,8,9")))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }else {
      $arr=array("0","1","2","22","23","25");
      if(in_array($status,$arr)){
        $inarray = 1;
      }else{
        $inarray = 0;
      }
      if($inarray == 0){
        $this->error('参数错误!', U('User/loglist'));
      }else{
        $all_loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "$status")))->where($where)->order('id desc')->count();
        $loglist = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "$status")))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
      }
    }

    $all_sy = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "1,2,8,9,21,22,23,25")))->getField("money", true);

    $all_sy = ypp_array_sum($all_sy);
    //$all_sy = (array_sum($all_sy) > 0) ? array_sum($all_sy) : 0;

    $all_hg = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => 3))->getField("money", true);
    $all_hg = (array_sum($all_hg) > 0) ? array_sum($all_hg) : 0;
    $this->assign("all_hg", $all_hg);

    $this->assign("all_sy", sctonum($all_sy+$all_hg));
   // $this->assign("status", $status);

    foreach($loglist as $k=>$v){
      $loglist[$k]['money'] = sctonum($loglist[$k]['money']);
      $msg = $v['msg'];
      $arr  = explode("，", $msg);
      if($v['type'] == 8 || $v['type'] == 9){
        $loglist[$k]['msg'] = $arr[0] .",". $arr[1];
      }else{
        $loglist[$k]['msg'] = $arr[1];
      }
    }
    $this->assign("loglist", $loglist);


    //ypp 分页
    $page_all = ceil($all_loglist/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign('is_mobile', $this->ismobile());
    $this->assign("count", $all_loglist);

    $this->display("User/loglist");
  }


  /*团队结构*/
  public function tdjg()
  {
    $id = I("id");
    $this->assign("thisid", $id);
    $now_list_num = M("Sys")->where(array("name"=> 'now_list_num'))->getField('value'); //当前轮次
    if ($id) {
      $user_id = M("Seatlist")->where(array("id" => $id,'list_num'=>$now_list_num))->getField("user_id");
      $username = M("User")->where(array("id" => $user_id))->getField("username");
    } else {
      $keyword = I("post.username");
      if ($keyword) {
        $this->assign("keyword", $keyword);
        $user_id = M("User")->where(array("username" => $keyword))->getField("id");
        if(!$user_id){
          $user_id = M("User")->where(array("username" => array("like", "%$keyword%")))->getField("id");
        }
        if (!$user_id) {
          $this->error('未查询到用户，请重新输入', U('User/tdjg'));
        } else {

          $ismychild = checkchild($user_id,trim(session('userId')));
          if($ismychild == 0){
            $this->success('您不能查看该用户的团队结构！', U('User/tdjg'));
            die;
          }


          $isseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->getField("id");
          if (!$isseat) {
            $this->error('该用户尚无捐赠记录！', U('User/tdjg'));
          }
        }
        $username = M("User")->where(array("id" => $user_id))->getField("username");
      } else {
        $user_id = trim(session('userId'));
        $isseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->getField("id");
        if (!$isseat) {
          $this->error('您还没有在本轮捐赠过！', U('User/index'));
        }
        $username = M("User")->where(array("id" => $user_id))->getField("username");
      }
    }


    $allmyseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->order('id asc')->select();
    $this->assign("allmyseat", $allmyseat);
    $this->assign("myseat_count", count($allmyseat));

    //我的座位信息
    if ($id) {
      $myseat = M("Seatlist")->where(array("id" => $id,'list_num'=>$now_list_num))->order('id asc')->limit(1)->select();
    } else {
      $myseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->order('id asc')->limit(1)->select();
    }

    //我的下线1座位信息
    $mytree1 = M("Seatlist")->where(array("line" => $myseat[0]['line'] + 1, "seat" => $myseat[0]['seat'] * 2 - 1,'list_num'=>$now_list_num))->select();
    foreach ($mytree1 as $k => $v) {
      $mytree1[$k]['username'] = M("User")->where(array("id" => $v['user_id']))->getField("username");
    }
    //我的下线2座位信息
    $mytree2 = M("Seatlist")->where(array("line" => $myseat[0]['line'] + 1, "seat" => $myseat[0]['seat'] * 2,'list_num'=>$now_list_num))->select();
    foreach ($mytree2 as $k => $v) {
      $mytree2[$k]['username'] = M("User")->where(array("id" => $v['user_id']))->getField("username");
    }
    $num1 = $this->sumteam($myseat[0]['line'], $myseat[0]['seat'],$now_list_num);
    $num2 = $this->sumteam($myseat[0]['line'] + 1, $myseat[0]['seat'] * 2 - 1,$now_list_num);
    $num3 = $this->sumteam($myseat[0]['line'] + 1, $myseat[0]['seat'] * 2,$now_list_num);

    $this->assign("num_1_left", $num1['left']);
    $this->assign("num_1_right", $num1['right']);

    $this->assign("num_2_left", $num2['left']);
    $this->assign("num_2_right", $num2['right']);

    $this->assign("num_3_left", $num3['left']);
    $this->assign("num_3_right", $num3['right']);

    $this->assign("myseat", $myseat);
    $this->assign("mytree1", $mytree1);
    $this->assign("mytree2", $mytree2);
    $this->assign("username", $username);
    $this->display("User/team");
  }


  /*推荐结构*/
  public function tjjg()
  {
    $username = I("post.username");

    if ($username) {
      $userid = M("User")->where(array("username" => $username))->getField("id");
      if(!$userid){
        $userid = M("User")->where(array("username" => array("like", "%$username%")))->getField("id");
      }
      if($userid){
        if($userid !=trim(session('userId'))){
          $ismychild = checkchild($userid,trim(session('userId')));
          //$ismychild = M("User")->where(array("id"=>$userid))->getField("id");
          if($ismychild == 0){
            $this->success('您不能查看该用户的推荐结构！', U('User/tjjg'));
            die;
          }
        }
      }else{
        $this->success('搜索结果不匹配，请更换搜索词！', U('User/tjjg'));
        die;
      }
      $parent = M("User")->where(array("id" => $userid))->select();
      foreach ($parent as $k => $v) {
        $haschild = M("User")->where(array("parent" => $v['id']))->getField("id");
        if ($haschild) {
          $parent[$k]['has'] = 1;
        } else {
          $parent[$k]['has'] = 0;
        }
      }
      $child_num = M("User")->where(array("parent" => $userid))->count();
      $this->assign('child_num', $child_num);
      $this->assign('keyword', $username);
      $this->assign('parent', $parent);
    } else {
      $userid = trim(session('userId'));
      $parent_id = I("get.parent");
      $child_num = M("User")->where(array("parent" => $userid))->count();
      $this->assign('child_num', $child_num);
      if ($parent_id) {
        $parent = M("User")->where(array("parent" => $parent_id))->select();
        foreach ($parent as $k => $v) {
          $haschild = M("User")->where(array("parent" => $v['id']))->getField("id");
          if ($haschild) {
            $parent[$k]['has'] = 1;
          } else {
            $parent[$k]['has'] = 0;
          }
        }
        $this->ajaxReturn($parent, 'JSON');
      } else {
        $parent = M("User")->where(array("id" => $userid))->select();
        foreach ($parent as $k => $v) {
          $haschild = M("User")->where(array("parent" => $v['id']))->getField("id");
          if ($haschild) {
            $parent[$k]['has'] = 1;
          } else {
            $parent[$k]['has'] = 0;
          }
        }
        $this->assign('parent', $parent);
      }
    }

    $this->display("User/childtree");
  }


  public function check()
  {
    if (IS_GET) {
      $parent = I("get.id");
      $user = M("User");
      $checkparent = $user->where("parent = $parent")->select();
      $this->ajaxReturn($checkparent, 'JSON'); /*json方式返回结果*/
    }
  }

  //提现页面
  public function tixian()
  {
    $user = M("User");
    $userId = trim(session('userId'));
    $txdz = M("User")->where(array("id"=>$userId))->getField("txdz");
    $this->assign('txdz', $txdz);
    $username = M("User")->where(array("id"=>$userId))->getField("username");
    $usermoney = $user->where("id = $userId")->getField("usermoney");
    $usermoney = sctonum($usermoney);
    $this->assign('usermoney', $usermoney);
    $tx_list = M("Myzc")->where(array("username"=>$username))->order("id desc")->select();
    $this->assign('tx_list', $tx_list);
    $this->display();
  }


  public function tx_done()
  {
    $userId = trim(session('userId'));
    $usermoney = M("User")->where(array("id"=>$userId))->getField("usermoney");
    $username = M("User")->where(array("id"=>$userId))->getField("username");
    $sxf = M("Sys")->where(array("name"=>"wlsxf"))->getField("value");//手续费
    $num = I("post.tx_num");
    $qbdz = I("post.qbdz");
   if($usermoney >= $num){


     //提现异常执行记录。
     $type = array(1,2,4,8,9,10,11,20,21,22,23,24,25,26);
     //log表money总和
     $log_money = M("Log")->where(array("user_id"=>$userId,"type"=>array("in",$type)))->sum("money");
     if($log_money < $num){
       $err["user"] = $username;
       $err["msg"] = "用户余额异常，提现失败！";
       $err["date"] = date("Y-m-s H:i:s");
       file_put_contents('tx_error.php', "<?php\r\nreturn " . var_export($err, true) . "?>", FILE_APPEND);
       $this->error("提现失败，请联系管理员！", U('User/tixian'));
     }




     $res = sendtrance($username, $qbdz, $num,$sxf);
     if($res['error']==0){
       $this->success('已成功提现！', U('User/tixian'));
     }else{
       $this->error($res['msg'], U('User/tixian'));
     }
   }else{
     $this->error("您的余额不足！", U('User/tixian'));
   }

  }




  public function tx_address()
  {
    $userid = trim(session('userId'));
    $new_txdz = I("post.new_txdz");
    $code = I("post.code");
    if(empty($new_txdz)){
      $this->error('请填写您的提现地址！', U('User/tixian'));
      die();
    }
    if(empty($code))
    {
      $this->error('请填写您的email安全码！', U('User/tixian'));
      die();
    }

    if($code == $_SESSION['em_code']){
      $data['txdz'] = $new_txdz;
      M("User")->where(array("id"=>$userid))->save($data);
      $_SESSION['em_code'] = '';
      $this->success('提现地址修改成功！', U('User/tixian'));
    }else{
      $this->error('您的email安全码不正确！', U('User/tixian'));
    }

  }


  //提现页面
  public function zhuanzeng()
  {
    $userId = trim(session('userId'));
    $user = M("User")->where(array("id"=>$userId))->find();
    $username = M("User")->where(array("id"=>$userId))->getField("username");
    $usermoney = M("User")->where("id = $userId")->getField("usermoney");
    $usermoney = sctonum($usermoney);
    $this->assign('usermoney', $usermoney);
    $zz_list = M("Log")->where(array("user_id"=>$userId,'type'=>31))->order("id desc")->select();
    foreach($zz_list as $k=>$v){
      $zz_list[$k]['zzuser'] =  $this->get_between($v['msg'],"转赠给","，");
    }
    $this->assign('user', $user);
    $this->assign('zz_list', $zz_list);
    $this->display();
  }
  function get_between($input,$start,$end){
    return substr($input,strlen($start)+strpos($input,$start),(strlen($input)-strpos($input,$end))*(-1));
  }

  public function zz_done()
  {
    $userId = trim(session('userId'));
    $user =  M("User")->where(array("id"=>$userId))->find();
    $usermoney = $user["usermoney"];
    $username = $user["username"];
    $sxf = M("Userclass")->where(array("class"=>$user['user_class']))->getField("bi_sxf");//手续费
    $num = I("post.tx_num");
    $realnum = $num + ($num*$sxf*0.01);
    $qbdz = I("post.qbdz");
    $user1 = M("User")->where(array("username"=>$qbdz))->find();
    if(!$user1){
      $this->error("转赠用户不存在！", U('User/zhuanzeng'));
    }
    if($usermoney >= $realnum){
      $u['usermoney'] = $usermoney-$realnum;
      $res = M("User")->where(array("id"=>$userId))->save($u);
      if($res){
        add_log($userId, -$num,-$realnum, 31, "转赠给".$qbdz."，".$num."，手续费".$num*$sxf*0.01);
        $u1['usermoney'] = $user1['usermoney']+$num;
        M("User")->where(array("id"=>$user1['id']))->save($u1);
        add_log($user1['id'], $num,0, 32, "获得$username"."转赠，$num");
      }
      $this->success('已成功转赠！', U('User/zhuanzeng'));

    }else{
      $this->error("您的余额不足！", U('User/zhuanzeng'));
    }

  }

  //充值页面
  public function chongzhi()
  {
    $userId = trim(session('userId'));
    $user = M("User")->where(array("id"=>$userId))->find();
    $username = M("User")->where(array("id"=>$userId))->getField("username");
    $usermoney = M("User")->where("id = $userId")->getField("usermoney");
    $usermoney = sctonum($usermoney);
    $this->assign('usermoney', $usermoney);
    $qbdz = M("User")->where("id = $userId")->getField("qbdz");
    $this->assign('qbdz', $qbdz);
    $cz_list = M("Log")->where(array("type"=>10,"user_id" => $userId,"status"=>1))->order("id desc")->select();
    foreach($cz_list as $k => $v){
      $cz_list[$k]['money'] = sctonum($cz_list[$k]['money']);
    }
    $this->assign('user', $user);
    $this->assign('cz_list', $cz_list);
    $this->display();
  }
  public function shoukuan(){
    $userId = trim(session('userId'));
    $qbdz = I("post.qbdz")?I("post.qbdz"):"";
    if(!$qbdz || $qbdz == ''){
      $this->error('请填写您的收款信息!', U('User/chongzhi'));
    }
    $u['qbdz'] = $qbdz;
    $res = M("User")->where(array("id"=>$userId))->save($u);
    if($res){
      $this->success('收款信息已保存!', U('User/chongzhi'));
    }else{
      $this->error('收款信息保存失败!', U('User/chongzhi'));
    }

  }


  public function tixian_list()
  {
    $tixian = M("Tixian");
    $userId = trim(session('userId'));
    $tixian = $tixian->where("userid ='$userId'")->select();
    $this->assign('tixian', $tixian);
    $this->display();
  }

  public function tixian_shenqing()
  {
    $tixian = M("Tixian");
    $user = M("User");
    $userId = trim(session('userId'));
    $username = $user->where("id = $userId")->getField("username");
    $phone = $user->where("id = $userId")->getField("phone");
    $zhifubao = I("post.zhifubao");
    $money = I("post.money");
    $date = date("Y-m-d H:i:s");
//  要插入提现记录的数据
    $data['userid'] = $userId;
    $data['username'] = $username;
    $data['phone'] = $phone;
    $data['zhifubao'] = $zhifubao;
    $data['apply_money'] = $money;
    $data['shenqing_date'] = $date;
    $res = $tixian->add($data);
    if ($res) {
      $this->success('申请成功，等待管理员确认！', U('User/User/index'));
      $usermoney = $user->where("id = $userId")->getField("usermoney");
      $data['usermoney'] = $usermoney - $money;
      $user->where("id=$userId")->save($data);
    }
  }

// 修改密码
  public function xgmm()
  {
    $userid = trim(session('userId'));
    $new_pass = I("post.new_pass");
    $code = I("post.code");

    if(empty($new_pass) || strlen($new_pass)<6){
      $this->error('请输入新密码，密码不能少于6位！');
      die;
    }
    if(empty($code)){
      $this->error('请输入email安全码！');
      die;
    }

    if($new_pass){
      if($code == $_SESSION['em_code']){
        $data['password'] = md5($new_pass);
        M("User")->where(array("id"=>$userid))->save($data);
        $_SESSION['em_code'] = '';
        $this->success('您的密码修改成功，请使用新密码重新登陆！', U('Index/login'));
      }else{
        $this->error('您的email安全码不正确！', U('User/index?xgmm=1'));
      }
    }else{
      $this->error('请填写您的新密码！', U('User/index?xgmm=1'));
    }
  }

  public function headimg_done()
  {
    $user = M("User");
    $userId = trim(session('userId'));
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 3145728;// 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'pdf');// 设置附件上传类型
    $upload->rootPath = './Uploads/'; // 设置附件上传根目录
    $upload->savePath = 'headimg'; // 设置附件上传（子）目录
    // 上传文件
    $info = $upload->upload();
    if (!$info) {// 上传错误提示错误信息
      $this->error($upload->getError());
    } else {// 上传成功
      foreach ($info as $file) {
        $bigimg = $file['savepath'] . $file['savename'];
      }
      $data['head_img'] = $bigimg;
      $res = $user->where("id=$userId")->save($data);
      if ($res) {
        $this->success('上传成功', U('User/index'));
      }
    }
  }


  public function ewm()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    $id = I("id");
    $userid = trim(session('userId'));
    if($id!=$userid){
      $this->error('参数错误！', U('User/ewm',array("id"=>$userid)));
    }

    $name = M("User")->where(array("id"=>$id))->getField("username");
    $allchild = M("User")->where(array("parent" => $id))->select();
    $child = M("User")->where(array("parent" => $id))->order("reg_time desc")->limit($page*$pagenum-$pagenum,$pagenum)->select();
    if ($child) {
      $this->assign('child', $child);
    }

    //ypp 分页
    $page_all = ceil(count($allchild)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($allchild));
    $this->assign('parent', $name);

$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$url =  $http_type . $_SERVER['HTTP_HOST'];

$this->assign('server', $url);
    $this->display();
  }

  //加入社群
  public function jrsq()
  {
    $jrsq = M("Sys")->where(array("name"=>"sqxx"))->getField("value");
    $this->assign('jrsq', $jrsq);
    $this->display();
  }
  //规则介绍
  public function gzjs()
  {
    $gzjs = M("Sys")->where(array("name"=>"gzjs"))->getField("value");
    $this->assign('gzjs', $gzjs);
    $this->display();
  }

  /* mai end 测试对接莱特币 */

  //二期，矿机列表
  public function kj_list()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $user_id = trim(session('userId'));
    $status = !empty(I("status")) ? I("status") : 0;
    $time = time();
    if(empty(I('page'))){
      $_SESSION["start_time"] = '';
      $_SESSION["end_time"] = '';
      $_SESSION["status"] = '';
    }

    if (I("post.reservatTime") || $_SESSION['start_time'] !="") {
      if(I("post.reservatTime")){
        $_SESSION['start_time'] = I("post.reservatTime");
      }
      $s_time = strtotime($_SESSION['start_time']); //开始日期
      $this->assign("start_time", $_SESSION['start_time']);
    }
    if (I("post.leaveTime") || $_SESSION['end_time'] !="") {
      if(I("post.leaveTime")){
        $_SESSION['end_time'] = I("post.leaveTime");
      }
      $e_time = strtotime($_SESSION['end_time']) + 86400;    //截至日期当天23：59：59
      $this->assign("end_time", $_SESSION['end_time']);
    }
    if (I("status") || $_SESSION['status'] !="") {
      if(I("status")){
        $_SESSION['status'] = I("status");
      }
      $this->assign("status", $_SESSION['status']);
    }

    if (!$s_time) {
      $s_time = 0;
    }
    if (!$e_time) {
      $e_time = time();
    }

    $where = array("addtime >= $s_time", "addtime <= $e_time","is_show = 1");
    if ($status == 0) {
      $all_kjlist =  M("Kuangji")->where($where)->order('id desc')->count();
      $kjlist = M("Kuangji")->where(array("type" => array("in", "1,2")))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    } else if ($status == 1) {
      $all_kjlist = M("Kuangji")->where(array("type" => 1))->where($where)->order('id desc')->count();
      $kjlist = M("Kuangji")->where(array("type" => 1))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    } else if ($status == 2) {
      $all_kjlist = M("Kuangji")->where(array("type" => 2))->where($where)->order('id desc')->count();
      $kjlist = M("Kuangji")->where(array("type" => 2))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }else {
      $this->error('参数错误!', U('User/kjlist'));
    }
//理财收益
    $all_sy = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "22,25")))->getField("amount", true);
    $all_sy = ypp_array_sum($all_sy);


    $this->assign("lc_sy", sctonum($all_sy));
    foreach ($kjlist as $k => $v) {
      $kjlist[$k]['sy'] = $v['day_num'] - $v['now_num'];
      $kjlist[$k]['sy_m'] = $v['day_money'] - $v['now_money'];
      if($v['price']>0){
        $kjlist[$k]['price'] = sctonum($v['price']);
      }else{
        $kjlist[$k]['price2_s'] = sctonum($v['price2_s']);
        $kjlist[$k]['price2_e'] = sctonum($v['price2_e']);
      }
    }
    // $this->assign("status", $status);
    $this->assign("kjlist", $kjlist);
    $this->assign('is_mobile', $this->ismobile());
    //ypp 分页
    $page_all = ceil($all_kjlist/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign('is_mobile', $this->ismobile());
    $this->assign("count", $all_kjlist);

    $this->display("User/kjlist");
  }

  //购买矿机
  public function buy_kj()
  {
    $userId = trim(session('userId'));
    $user = M("User")->where(array("id"=>$userId))->select();
    $kj_id = I("get.kjid")>0?I("get.kjid"):I("post.kjid");
    if(!$kj_id){
      $this->error('参数错误！', U('User/User/index'));
    }
    $kj = M("Kuangji")->where(array("id"=>$kj_id,"is_show"=>1))->select();
    if(!$kj){
      $this->error('该商品不存在！', U('User/User/index'));
    }else{
      $max_num = $kj[0]['max_num'];
      $user_thiskjnum =  M("Userkj")->where(array("user_id"=>$userId,"kj_id"=>$kj_id,"status"=>0))->count();
      if($user_thiskjnum >= $max_num){
        $this->error('已达该矿机最大可运行数量！', U('User/User/index'));
      }

      $kjprice = $kj[0]['price'];

      $price = $kjprice>0?$kjprice:I("post.buy_price");
      if($user[0]['usermoney'] >= $price){
        if($kjprice){
          $data['usermoney'] = $user[0]['usermoney'] - $kjprice;
        }else{
          $data['usermoney'] = $user[0]['usermoney'] - I("post.buy_price");
        }

        M("User")->where(array("id"=>$userId))->save($data);//更新用户余额
        //插入记录
        if($kjprice){
          add_log($userId, -$kjprice,0, 20, "购买矿机-".$kjprice);
        }
        //用户添加矿机
        $res = ypp_addkj($userId,$kj_id,$price);
        if($res == 'true'){
          $this->success('恭喜！投资商品成功！', U('User/User/mykj'));
        }else{
          $this->error('投资商品失败！', U('User/User/kj_list'));
        }
      }else{
        $this->error('您的余额不足，请先充值！', U('User/User/chongzhi'));
      }
    }
  }

  //我的矿机列表
  public function mykj()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $user_id = trim(session('userId'));
    $status = !empty(I("status")) ? I("status") : 0;

    if(empty(I('page'))){
      $_SESSION["start_time"] = '';
      $_SESSION["end_time"] = '';
      $_SESSION["status"] = '';
    }

    if (I("post.reservatTime") || $_SESSION['start_time'] !="") {
      if(I("post.reservatTime")){
        $_SESSION['start_time'] = I("post.reservatTime");
      }
      $s_time = strtotime($_SESSION['start_time']); //开始日期
      $this->assign("start_time", $_SESSION['start_time']);
    }
    if (I("post.leaveTime") || $_SESSION['end_time'] !="") {
      if(I("post.leaveTime")){
        $_SESSION['end_time'] = I("post.leaveTime");
      }
      $e_time = strtotime($_SESSION['end_time']) + 86400;    //截至日期当天23：59：59
      $this->assign("end_time", $_SESSION['end_time']);
    }
    if (I("status") || $_SESSION['status'] !="") {
      if(I("status")){
        $_SESSION['status'] = I("status");
      }
      $this->assign("status", $_SESSION['status']);
    }

    if (!$s_time) {
      $s_time = 0;
    }
    if (!$e_time) {
      $e_time = time();
    }
    $where = array("addtime >= $s_time", "addtime <= $e_time" ,"user_id = $user_id");
    if ($status == 0) {
      $all_kjlist =  M("Userkj")->where($where)->order('id desc')->count();
      $kjlist = M("Userkj")->where(array("type" => array("in", "1,2")))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    } else if ($status == 1) {
      $all_kjlist = M("Userkj")->where(array("type" => 1))->where($where)->order('id desc')->count();
      $kjlist = M("Userkj")->where(array("type" => 1))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    } else if ($status == 2) {
      $all_kjlist = M("Userkj")->where(array("type" => 2))->where($where)->order('id desc')->count();
      $kjlist = M("Userkj")->where(array("type" => 2))->where($where)->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }else {
      $this->error('参数错误!', U('User/mykj'));
    }
    //理财收益
    $all_sy = M("Log")->where(array("user_id" => $user_id,"money"=>array("neq",0), "type" => array("in", "22,25")))->getField("amount", true);
    $all_sy = ypp_array_sum($all_sy);


    $this->assign("lc_sy", sctonum($all_sy));


    foreach($kjlist as $k=>$v){
      $kj = M("Kuangji")->where(array("id"=>$v['kj_id']))->find();
      $kjlist[$k]['price'] = sctonum($v['price']);
      $kjlist[$k]['name'] = $kj["name"];
      $kjlist[$k]['shifang'] = $kj["shifang"];
      $kjlist[$k]['cycle'] = $kj["cycle"];
      $kjlist[$k]['yunxing'] = intval((time()-$v['addtime'])/86400);
      $kjlist[$k]['day_num'] = $kj["day_num"];
      $bl = $kj["tqsf"];
      $kjlist[$k]['tqsf'] = $bl;
      $kjlist[$k]['lilv'] = $kj["lilv"];
      $kjlist[$k]['kjtime'] = $v['addtime'];

    }
    $this->assign("kjlist", $kjlist);


    //ypp 分页
    $page_all = ceil($all_kjlist/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign('is_mobile', $this->ismobile());
    $this->assign("count", $all_kjlist);

    $this->display("User/mykj");
  }

  //退本金
  public function tui(){
    $user_id = trim(session('userId'));
    $id = I("id");
    $user = M("User")->where(array("id"=>$user_id))->select();
    if(!$id){
      $this->error('参数错误！', U('User/User/mykj'));
    }else{
      $is_kj = M("Userkj")->where(array("id"=>$id,"user_id"=>$user_id))->select();
      if(!$is_kj){
        $this->error('参数错误！', U('User/User/mykj'));
      }else{
        $bl = M("Kuangji")->where(array("id"=>$is_kj[0]['kj_id']))->getField("tqsf");
        //退还比例
        $thbl = 100-$bl;
        $price = $is_kj[0]['price']*($thbl/100);//提前退购，扣除本金比例

        $type = M("Kuangji")->where(array("id"=>$is_kj[0]['kj_id']))->getField("type");
        //已得收益
        if($type == 1){
          $sy = M("Log")->where(array("user_id"=>$user_id,"type"=>22,"licai_id"=>$id))->Sum("amount");
          $all_sy = $sy;
        }else{
          $all_sy = 0;
        }

        $datausermoney = $user[0]['usermoney'] + $price - $all_sy;
        if($datausermoney < 0){
          $this->error('余额不足，无法完成该操作！', U('User/User/mykj'));
        }else{
          $data['usermoney'] = $datausermoney;
        }
        if($datausermoney != $user[0]['usermoney']){
          $res = M("User")->where(array("id"=>$user_id))->save($data);
        }else{
          $res = 1;
        }
        //$data['freeze_money'] = $user[0]['freeze_money'] - $hg;
        if($res){
          $dkj['status'] = 1;
          M("Userkj")->where(array("id"=>$id))->save($dkj);
          //插入记录
          add_log($user_id, $price,0, 24, "提前释放本金，退还".$thbl."%本金");
          if($type == 1){
            add_log($user_id, -$all_sy,0, 26, "提前释放本金，扣除已得收益");
          }
          //add_log($user_id, -$hg,0, 3, "扣除回购-提前释放本金");
          $this->success('本金已成功退还至您的余额！', U('User/User/mykj'));
        }
      }
    }
  }

  //交易大厅
  public function market(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $user_id = trim(session('userId'));
    $type = I('type')? I('type'):1;
    $username = I('username')? I('username'):"";
    if($username != ''){
      $uid = M("User")->where(array("username"=>$username))->getField("id");
      $uid = $uid?$uid:0;
      $where = array("status"=>0,'type'=>$type,'user_id'=>$uid);
    }else{
      $where = array("status"=>0,'type'=>$type);
    }

    $user = M("User")->where(array("id"=>$user_id))->find();
    $all_list =  M("Market")->where($where)->where(array("is_del"=>0))->order('id desc')->count();
    $list = M("Market")->where($where)->where(array("is_del"=>0))->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    foreach($list as $k=>$v){
      $list[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField('username');
    }
    $this->assign("user", $user);
    $this->assign("list", $list);
    //ypp 分页
    $page_all = ceil($all_list/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $all_list);
    $this->assign("type", $type);
    $this->assign("username", $username);
    $this->display("User/market");
  }
  //购买矿机币
  public function marketbuy(){
    $id = I('id');
    $user_id = trim(session('userId'));
    $user = M("User")->where(array("id"=>$user_id))->find();
    $sale = M("Market")->where(array("id"=>$id))->find();
    if($sale && $sale['status'] == 0){

      if($sale['user_id'] == $user_id){
        $this->error('不能购买自己售卖的商品！',U('User/market'));
        die;
      }
      //使用USDT
      if($sale['num'] > 10 && $sale['type'] == 1){
        $usdt_lv = M("Sys")->where(array("name"=>"usdt_lv"))->getField("value");
        $usdt = $sale['num']*$usdt_lv;
        if($user['sellmoney'] < $usdt){
          $this->error('USDT余额不足！',U('User/market'));
          die;
        }else{
          $d['status'] = 2;
          $d['buy_id'] = $user_id;
          $d['sale_time'] = time();
          M("Market")->where(array("id"=>$id))->save($d);
          $u['sellmoney'] = $user['sellmoney'] - $usdt;
          $u['usermoney'] = $user['usermoney'] + $sale['num'];
          M("User")->where(array("id"=>$user_id))->save($u);
          add_log($sale['buy_id'], $sale['num'],0, 31, "币交易，买入".$sale['num'].",扣除".$usdt."USDT");
          $user1 = M("User")->where(array("id"=>$sale['user_id']))->find();
          $u1['sellmoney'] = $user1['sellmoney'] + $usdt;
          M("User")->where(array("id"=>$sale['user_id']))->save($u1);
          add_log($sale['user_id'], -$sale['num'],$sale['realnum'], 30, "币交易，售出".$sale['num']."，获取".$usdt."USDT");
          $this->success('购买成功！',U('User/mymarket'));

        }

      }else{
        $d['status'] = 1;
        $d['buy_id'] = $user_id;
        M("Market")->where(array("id"=>$id))->save($d);
        $this->success('购买成功！请打款后上传支付凭证！',U('User/mymarket'));
      }



    }else{
      $this->error('记录不存在或已售出！', U('User/market'));
    }
  }
  public function dosale(){
    $user_id = trim(session('userId'));
    $user = M("User")->where(array("id"=>$user_id))->find();
    $min_price = M("Sys")->where(array("name"=>'min_price'))->getField("value");
    $max_num = M("Sys")->where(array("name"=>'max_num'))->getField("value");
    $day_num = M("Sys")->where(array("name"=>'day_num'))->getField("value");

    $today = strtotime(date("Ymd",time()));
    $d['user_id'] = $user_id;
    $d['price'] = I('price')?round(I('price'),2):0;
    $d['num']= I('num')?ceil(I('num')):0;
    $d['msg']= I('msg')?I('msg'):"";
    $d['type'] = I('type')?I('type'):1;
    if($d['price'] == 0 || $d['price'] < 0){
      $this->error('请按规则填写单价！', U('User/market'));
    }elseif($d['price'] < $min_price){
      $this->error("最低售价($min_price)！", U('User/market'));
    }elseif($d['num'] == 0 || $d['num'] < 0){
      $this->error('请按规则填写出售数量！', U('User/market'));
    }elseif($d['msg'] == ''){
      $this->error('请填写您的收款信息！', U('User/market'));
    }else{
      if($d['type'] == 1){
        if($d['num'] > $max_num){
          $this->error('超过售卖交易限制（最大'.$max_num.'）！', U('User/market'));
        }
        $sale = M('Market')->where(array('user_id'=>$user_id,'type'=>1,'status'=>0))->find();
        if($sale){
          $this->error('您有售卖交易正在进行！', U('User/market'));
        }
        $salecount = M('Market')->where(array('user_id'=>$user_id,'type'=>1,'add_time'=>array('gt',$today)))->count();

        if($salecount > $day_num){
          $this->error('一天只能发布'.$day_num.'单售卖交易！', U('User/market'));
        }
        $buyed = M('Market')->where(array('buy_id'=>$user_id,'type'=>1,'sale_time'=>array("gt",$today)))->find();
        if($buyed){
          $this->error('当日有过购买记录，无法进行售卖！', U('User/market'));
        }
      }
    }


    $d['total'] = $d['price']*$d['num'];
    $d['add_time'] = time();


      //查询A系统会员等级，冻结币+手续费
      $url = "http://09xingfuba.tophp.cn/mobile/index.php?act=get_userclass";
      $da['username'] = $user['username'];
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
      if (!empty($da)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $da);
      }
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      $output = curl_exec($curl);
      curl_close($curl);
      $data = json_decode($output,true);
      $lv = $data['b_sxf'];
      $realnum = $d['num']+($d['num']*$lv*0.01);
      if($realnum > $user['usermoney']){
        $this->error('超出您的最大可交易数量！', U('User/market'));
      }

      $u['usermoney'] = $user['usermoney']-$realnum;
      $u['freeze_money'] = $user['freeze_money']+$realnum;

      $res = M("User")->where(array("id"=>$user_id))->save($u);
    if($res){
      $d['realnum'] = $realnum;
      M("Market")->add($d);
      $this->success('售卖信息已成功提交！', U('User/market'));
    }else{
      $this->error('信息提交失败！', U('User/market'));
    }
  }


  //交易大厅
  public function mymarket(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $user_id = trim(session('userId'));
    $status = !empty(I('status'))?I('status'):1;
    if($status == 1){//出售
      $where = array("user_id"=>$user_id);
    }else{ //购买
      $where = array("buy_id"=>$user_id);
    }
    $user = M("User")->where(array("id"=>$user_id))->find();
    $all_list =  M("Market")->where($where)->where(array("is_del"=>0))->order('id desc')->count();
    $list = M("Market")->where($where)->where(array("is_del"=>0))->order('id desc')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    foreach($list as $k=>$v){
      $list[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField('username');
      $list[$k]['add_time'] = $v['add_time']>0 ? date('Y/m/d H:i:s',$v['add_time']):0;
      $list[$k]['sale_time'] = $v['sale_time']>0 ? date('Y/m/d H:i:s',$v['sale_time']):0;
    }
    $this->assign("user", $user);
    $this->assign("list", $list);
    //ypp 分页
    $page_all = ceil($all_list/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $all_list);
    $this->assign("status", $status);
    $this->display("User/mymarket");
  }
  public function dobuy(){
    $id = I('mid');
    $d['picture'] = "xxxx";
    M("Market")->where(array("id"=>$id))->save($d);
    $this->success('凭证已上传，等待卖家确认！',U('User/mymarket'));
  }
  //修改售卖信息
  public function editmarket(){
    $type = I('type');
    $id = I('id');
    $user_id = trim(session('userId'));
    $user =  M("User")->where(array("id"=>$user_id))->find();
    //取消售卖
    if($type == 'cancel_sale'){
      $d['is_del'] = 1;
      M("Market")->where(array("id"=>$id))->save($d);
      $sale =  M("Market")->where(array("id"=>$id))->find();
      $u['usermoney'] = $user['usermoney'] + $sale['realnum'];
      $u['freeze_money'] = $user['freeze_money'] - $sale['realnum'];
      M("User")->where(array("id"=>$user_id))->save($u);
      $this->success('售卖已取消！',U('User/mymarket'));
    }
    //未付款
    if($type == 're_sale'){
      $d['status'] = 0;
      $d['buy_id'] = 0;
      M("Market")->where(array("id"=>$id))->save($d);
      $this->success('超时未支付，已重新开启售卖！',U('User/mymarket'));
    }
    //已付款
    if($type == 'is_sale'){
      $d['status'] = 2;
      $d['sale_time'] = time();
      M("Market")->where(array("id"=>$id))->save($d);
      $sale =  M("Market")->where(array("id"=>$id))->find();

      //扣除卖家余额
      $u['freeze_money'] = $user['freeze_money'] - $sale['realnum'];
      M("User")->where(array("id"=>$user_id))->save($u);
      add_log($user_id, -$sale['num'],$sale['realnum'], 30, "币交易，售出".$sale['num']."，手续费".($sale['realnum']-$sale['num']));

      //买家增加余额
      $user1 =  M("User")->where(array("id"=>$sale['buy_id']))->find();
      $u1['usermoney'] = $user1['usermoney'] + $sale['num'];
      M("User")->where(array("id"=>$sale['buy_id']))->save($u1);
      add_log($sale['buy_id'], $sale['num'],0, 31, "币交易，买入".$sale['num']);

      $this->success('交易已完成！',U('User/mymarket'));
    }
  }


  function sumteam($line, $seat,$now_list_num)
  {
    $newline = $line;  //初始行数
    $newseat = $seat;   //初始位置
    $num_l = 0;
    $num_r = 0;
    $newseat_l = array($newseat * 2 - 1);
    $newseat_r = array($newseat * 2);
    $seat_l = $newseat_l;
    $seat_r = $newseat_r;
    $line_l = $newline;
    $line_r = $newline;
    /*统计左边总个数*/
    $is_l = true;
    while ($is_l == true) {
      $seat1 = array();
      foreach ($seat_l as $k => $v) {
        $treeid = M("Seatlist")->where(array("line" => $line_l + 1, "seat" => $v,"list_num"=>$now_list_num))->getField("id");
        if ($treeid) {
          $num_l += 1;
          $seat1[] = $v * 2 - 1;
          $seat1[] = $v * 2;
        } else {
          $is_l = false;
        }
      }
      $seat_l = $seat1;
      $line_l = $line_l + 1;
    }
    /*统计右边总个数*/
    $is_r = true;
    while ($is_r == true) {
      $seat2 = array();
      foreach ($seat_r as $k => $v) {
        $treeid = M("Seatlist")->where(array("line" => $line_r + 1, "seat" => $v,"list_num"=>$now_list_num))->getField("id");
        if ($treeid) {
          $num_r += 1;
          $seat2[] = $v * 2 - 1;
          $seat2[] = $v * 2;
        } else {
          $is_r = false;
        }
      }
      $seat_r = $seat2;
      $line_r = $line_r + 1;
    }
    $num = array("left" => $num_l, "right" => $num_r);
    return $num;
  }

  function ismobile()
  {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
      return true;

    //此条摘自TPM智能切换模板引擎，适合TPM开发
    if (isset ($_SERVER['HTTP_CLIENT']) && 'PhoneClient' == $_SERVER['HTTP_CLIENT'])
      return true;
    //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
      //找不到为flase,否则为true
      return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
    //判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
      $clientkeywords = array(
          'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
      );
      //从HTTP_USER_AGENT中查找手机浏览器的关键字
      if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
        return true;
      }
    }
    //协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
      // 如果只支持wml并且不支持html那一定是移动设备
      // 如果支持wml和html但是wml在html之前则是移动设备
      if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
        return true;
      }
    }
    return false;

  }

  //清除用户数据
  public function cleanuser()
  {
    $uid = I("uid");
    $data['userclass'] = 1;
    $data['usermoney'] = 1000;
    $data['freeze_money'] = 0;
    M("User")->where(array("id"=>$uid))->save($data);

    M("Log")->where(array("user_id"=>$uid))->delete();
    die("该用户信息已清除");
  }
    public function userspelic()
    {
        $this->display();
    }

    public function spelicadd()
    {
        $user_id = trim(session('userId'));
        $username = I('username');
        $usercard = I('usercard');
        $usertel  = I('usertel');
        $userzfb  = I('userzfb');
        $img1    = $_FILES['img1']?$_FILES['img1']:'';

        $row = $this->upload($img1);
        var_dump($row);exit;


        $checkcard = $this->isIdCard($usercard);
        if($checkcard==false){
            $this->error('请填写正确的身份证号！', U('User/userspelic'));
        }
        $checktel = $this->checkPhoneNumber($usertel);
        if($checktel==false){
            $this->error('请填写正确的手机号！', U('User/userspelic'));
        }
        $data['userId'] = $user_id;
        $data['username'] = $username;
        $data['usermoney'] = '';
        $data['sellmoney'] = '';
        $data['usercard'] = $usercard;
        $data['usertel'] = $usertel;
        $data['userzfb'] = $userzfb;

    }


    public   function isIdCard($number) {

        $sigma = '';
        //加权因子
        $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        //校验码串
        $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        //按顺序循环处理前17位
        for ($i = 0;$i < 17;$i++) {


            //提取前17位的其中一位，并将变量类型转为实数
            $b = (int) $number{$i};


            //提取相应的加权因子
            $w = $wi[$i];



            //把从身份证号码中提取的一位数字和加权因子相乘，并累加 得到身份证前17位的乘机的和
            $sigma += $b * $w;
        }
        //echo $sigma;die;
        //计算序号  用得到的乘机模11 取余数
        $snumber = $sigma % 11;
        //按照序号从校验码串中提取相应的余数来验证最后一位。
        $check_number = $ai[$snumber];

        if ($number{17} == $check_number) {

            return true;
        } else {

            return false;
        }
    }

    function checkPhoneNumber($phone_number)
    {
        //@2017-11-25 14:25:45 https://zhidao.baidu.com/question/1822455991691849548.html
        //中国联通号码：130、131、132、145（无线上网卡）、155、156、185（iPhone5上市后开放）、186、176（4G号段）、175（2015年9月10日正式启用，暂只对北京、上海和广东投放办理）,166,146
        //中国移动号码：134、135、136、137、138、139、147（无线上网卡）、148、150、151、152、157、158、159、178、182、183、184、187、188、198
        //中国电信号码：133、153、180、181、189、177、173、149、199
        $g = "/^1[34578]\d{9}$/";
        $g2 = "/^19[89]\d{8}$/";
        $g3 = "/^166\d{8}$/";
        if (preg_match($g, $phone_number)) {
            return true;
        } else if (preg_match($g2, $phone_number)) {
            return true;
        } else if (preg_match($g3, $phone_number)) {
            return true;
        }

        return false;

    }


    public function upload($img){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = 'images/';//保存的路径
        $upload->rootPath = './'; // 设置附件上传根目录  必须设置
//                dump($upload->savePath);exit;
        // 上传单个文件
        $info = $upload->uploadOne($_FILES['thumb']);
//            dump($info);exit;
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
//                    dump($info);exit;
            $data['thumb'] = $info['savepath'].$info['savename'];
        }
    }


    function get_file_name($len)//获取一串随机数字，用于做上传到数据库中文件的名字
    {
        $new_file_name = 'A_';
        $chars = "1234567890qwertyuiopasdfghjklzxcvbnm";//随机生成图片名
        for ($i = 0; $i < $len; $i++) {
            $new_file_name .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $new_file_name;
    }
}
